package com.admin.utils.encrypt.aes;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.springframework.stereotype.Component;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.spec.InvalidParameterSpecException;

/**
 * AES解密，用于解密微信登录的用户信息
 *
 * @Author: Liuanshou
 * @Date: 2021/4/14 16:12
 * @Version:
 * @Description: com.shangcheng.utils
 */
@Component
@Slf4j
public class AesCbcUtil {
    static {
        Security.addProvider(new BouncyCastleProvider());
    }
    /**
     * AES解密
     *
     * @param data           //密文，被加密的数据
     * @param key            //秘钥
     * @param iv             //偏移量
     * @param encodingFormat //解密后的结果需要进行的编码
     * @return
     * @throws Exception
     */
    public static String decrypt(String data, String key, String iv, String encodingFormat) throws Exception {
//        initialize();

        //被加密的数据
        byte[] dataByte = Base64.decodeBase64(data.getBytes());
        //加密秘钥
        byte[] keyByte = Base64.decodeBase64(key.getBytes());
        //偏移量
        byte[] ivByte = Base64.decodeBase64(iv.getBytes());


        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");

            SecretKeySpec spec = new SecretKeySpec(keyByte, "AES");

            AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES");
            parameters.init(new IvParameterSpec(ivByte));

            cipher.init(Cipher.DECRYPT_MODE, spec, parameters);// 初始化

            byte[] resultByte = cipher.doFinal(dataByte);
            if (null != resultByte && resultByte.length > 0) {
                String result = new String(resultByte, encodingFormat);
                return result;
            }
            return null;
        } catch (NoSuchAlgorithmException e) {
           log.info("NoSuchAlgorithmException:{}",e.getMessage());
        } catch (NoSuchPaddingException e) {
            log.info("NoSuchPaddingException:{}",e.getMessage());
        } catch (InvalidParameterSpecException e) {
            log.info("InvalidParameterSpecException:{}",e.getMessage());
        } catch (InvalidKeyException e) {
            log.info("InvalidKeyException:{}",e.getMessage());
        } catch (InvalidAlgorithmParameterException e) {
            log.info("InvalidAlgorithmParameterException:{}",e.getMessage());
        } catch (IllegalBlockSizeException e) {
            log.info("IllegalBlockSizeException:{}",e.getMessage());
        } catch (BadPaddingException e) {
            log.info("BadPaddingException:{}",e.getMessage());
        } catch (UnsupportedEncodingException e) {
            log.info("UnsupportedEncodingException:{}",e.getMessage());
        }

        return null;
    }
}
